<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="btnCancel">
    <!-- 表单 -->
    <el-form
      ref="addEmployee"
      :model="formData"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width: 50%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width: 50%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 50%"
          placeholder="请选择入职时间"
          format="yyyy 年 MM 月 dd 日"
          type="date"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 50%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in EmployeeEnum.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 50%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          style="width: 50%"
          placeholder="请选择部门"
          @focus="getDepartments"
        />
        <!-- 放置一个树形组件 -->
        <el-tree
          v-if="showTree"
          v-loading="loading"
          :data="treeData"
          :props="{ label: 'name' }"
          :default-expand-all="true"
          @node-click="selectNode"
        ></el-tree>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 50%"
          placeholder="请选择转正时间"
          format="yyyy 年 MM 月 dd 日"
          type="date"
        />
      </el-form-item>
    </el-form>

    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
// import EmployeeEnum from "@/api/constant/employees";
// 获取组织架构的数据接口
import { getDepartments } from "@/api/departments";
// 将列表数据转换为树形数据的方法
import { tranListToTreeData } from "@/utils";
//数据
import EmployeeEnum from "@/api/constant/employees";
// 新增接口
import { addEmployee } from "@/api/employees";
export default {
  name: "HrsaasAddEmployee",
  props: {
    showDialog: {
      type: Boolean, //接受一个布尔值
      default: false, //默认值为false
    },
  },
  data() {
    return {
      // // 定义表单数据
      formData: {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: new Date(), //入职时间默认为当前时间
        correctionTime: "",
      },
      rules: {
        username: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" },
          {
            min: 1,
            max: 4,
            message: "用户姓名为1-4位",
            trigger: "blur",
          },
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        formOfEmployment: [
          { required: true, message: "聘用形式不能为空", trigger: "blur" },
        ],
        workNumber: [
          { required: true, message: "工号不能为空", trigger: "blur" },
        ],
        departmentName: [
          { required: true, message: "部门不能为空", trigger: "change" },
        ],
        timeOfEntry: [{ required: true, message: "入职时间", trigger: "blur" }],
      },
      treeData: [], //定义一个数组来接受树形结构
      showTree: false, //默认不显示树形组件
      loading: false, //加上进度条
      EmployeeEnum: EmployeeEnum, //把引入的数据变成date 里面的数据
    };
  },
  components: {},
  directives: {},
  mounted() {},
  methods: {
    // 当选择部门input框获取焦点的时候
    async getDepartments() {
      this.loading = true;
      this.showTree = true;
      //调用获取组织框架的接口  把数据接受过来
      const { depts } = await getDepartments();
      //depts 是一个数组 他需要转化成树形结构 才可以被 el-tree 显示
      //tranListToTreeData(depts,'')  传递一个数组depts vulue值
      this.treeData = tranListToTreeData(depts, "");
      this.loading = false;
      //这里不用重置表单 因为 关闭弹层 直接触发了close 事件
    },
    // 树形控件节点被点击时的回调
    selectNode(node) {
      // console.log(node);
      // 把部门名称赋值给当前 部门
      this.formData.departmentName = node.name;
      // 关闭树形组件
      this.showTree = false;
    },
    // 点击取消
    btnCancel() {
      // 重置表单
      this.formData = {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: "", //入职时间默认为当前时间
        correctionTime: "",
      };
      this.$refs.addEmployee.resetFields(); //移除表单的验证规则
      // 通知父组件关闭弹框
      this.$emit("update:showDialog", false);
      // 'update:props 名称 这么写的话 可以在父组件 直接用sync 修饰符
    },
    // 点击确认
    async btnOk() {
      // 验证表单
      try {
        await this.$refs.addEmployee.validate();
        // 验证成功
        await addEmployee(this.formData); //调用新增接口
        // 通知父组件更新数据
        // console.log( this.$parent);  拿到父组件的实例
        this.$parent.getEmployeeList && this.$parent.getEmployeeList(); //直接调用父组件的更新方法
        this.$parent.showDialog = false; //关闭弹框
        this.$message.success("添加成功");
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style scoped>
/* div {
  display: flex;
} */
</style>